#!/bin/bash

# Slackware build script for magnus

# Written by B. Watson (yalhcru@gmail.com)

# Licensed under the WTFPL. See http://www.wtfpl.net/txt/copying/ for details.

# This thing is more flexible and featureful than mag, but slower and
# more resource-intensive (due to being written in python, I guess).

# Notes:

# The default refresh interval is 250ms, or 4 frames/sec. This is
# painful to use. On my not-very-powerful machine, 15ms (67fps) is
# fine. I'm patching the code to set the default to 30ms. Also, the
# man page and --help output claim the default is 120ms...

# This thing draws its own window titlebar and decorations, which look
# and behave different from *everything else* I use. Very annoying,
# and I'm told that's the direction the GTK/Gnome crowd is moving
# towards, especially in GTK+4.

# The default behaviour is to only update the magnified view when the
# mouse moves, which makes this pretty useless IMO. Upstream added
# a patch in git that adds a --force-refresh option, which makes it
# always update. What I'm doing here: patch it so it always refreshes,
# without having to pass an argument.

cd $(dirname $0) ; CWD=$(pwd)

PRGNAM=magnus
VERSION=${VERSION:-1.0.3}
BUILD=${BUILD:-1}
TAG=${TAG:-_SBo}
PKGTYPE=${PKGTYPE:-tgz}

if [ -z "$ARCH" ]; then
  case "$( uname -m )" in
    i?86) ARCH=i586 ;;
    arm*) ARCH=arm ;;
       *) ARCH=$( uname -m ) ;;
  esac
fi

if [ ! -z "${PRINT_PACKAGE_NAME}" ]; then
  echo "$PRGNAM-$VERSION-$ARCH-$BUILD$TAG.$PKGTYPE"
  exit 0
fi

CWD=$(pwd)
TMP=${TMP:-/tmp/SBo}
PKG=$TMP/package-$PRGNAM
OUTPUT=${OUTPUT:-/tmp}

if [ "$ARCH" = "i586" ]; then
  SLKCFLAGS="-O2 -march=i586 -mtune=i686"
  LIBDIRSUFFIX=""
elif [ "$ARCH" = "i686" ]; then
  SLKCFLAGS="-O2 -march=i686 -mtune=i686"
  LIBDIRSUFFIX=""
elif [ "$ARCH" = "x86_64" ]; then
  SLKCFLAGS="-O2 -fPIC"
  LIBDIRSUFFIX="64"
else
  SLKCFLAGS="-O2"
  LIBDIRSUFFIX=""
fi

set -e

rm -rf $PKG
mkdir -p $TMP $PKG $OUTPUT
cd $TMP
rm -rf $PRGNAM-$VERSION
tar xvf $CWD/$PRGNAM-$VERSION.tar.gz
cd $PRGNAM-$VERSION
chown -R root:root .
find -L .  -perm /111 -a \! -perm 755 -a -exec chmod 755 {} \+ -o \
        \! -perm /111 -a \! -perm 644 -a -exec chmod 644 {} \+

# always refresh even if the mouse hasn't moved.
sed -i '/def *poll/s,=False,=True,' $PRGNAM

# set the default refresh rate to something usable.
REFRESH=${REFRESH:-30}
sed -i -e '/refresh_interval *=/s,250,'$REFRESH',' \
       -e '/--refresh-interval=/s,120,'$REFRESH',' \
       $PRGNAM
sed -i 's,120ms,'$REFRESH',' data/$PRGNAM.1

python3 setup.py install --root=$PKG

mkdir -p $PKG/usr/doc/$PRGNAM-$VERSION
cp -a README.md LICENSE $PKG/usr/doc/$PRGNAM-$VERSION
cat $CWD/$PRGNAM.SlackBuild > $PKG/usr/doc/$PRGNAM-$VERSION/$PRGNAM.SlackBuild

mv $PKG/usr/share/man $PKG/usr/man
gzip -9 $PKG/usr/man/man?/*

# doesn't ship an icon, expects to uses a system icon called "zoom-best-fit".
# we have such an icon in the elementary-xfce theme, include a symlink here.
mkdir -p $PKG/usr/share/icons/hicolor/48x48/apps
ln -s ../../../elementary-xfce/actions/48/zoom-best-fit.png \
      $PKG/usr/share/icons/hicolor/48x48/apps

mkdir -p $PKG/install
cat $CWD/slack-desc > $PKG/install/slack-desc
cat $CWD/doinst.sh > $PKG/install/doinst.sh

cd $PKG
/sbin/makepkg -l y -c n $OUTPUT/$PRGNAM-$VERSION-$ARCH-$BUILD$TAG.$PKGTYPE
